package util.collection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * @author yangfan
 * @description 基本集合操作工具类
 * @date 2018/5/16 10:28
 **/

public class CollectionUtil {

	private static Logger LOGGER = LoggerFactory.getLogger(CollectionUtil.class);

	private CollectionUtil() {
		throw new AssertionError(getClass().getName() + " has no default instances.");
	}

	/**
	 * 返回对象List某个属性的List
	 *
	 * @param iterator     需要迭代的list
	 * @param propertyName 封装的属性名
	 * @return 封装好的list
	 */
	public static <T, U> List getPropertyList(Iterator<T> iterator, String propertyName) {
		List<U> propertyList = new LinkedList<>();
		try {
			while (iterator.hasNext()) {
				T next = iterator.next();
				if (Objects.nonNull(next)) {
					Class<?> beanClass = next.getClass();
					Field field = beanClass.getDeclaredField(propertyName);
					field.setAccessible(true);
					propertyList.add((U) field.get(next));
				}
			}
		} catch (NoSuchFieldException | IllegalAccessException e) {
			e.printStackTrace();
		}
		return propertyList;
	}

}
